﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp.Models
{
    internal class WordRecognizer
    {
        private readonly OcrProcessor _ocrProcessor;

        public WordRecognizer(OcrProcessor ocrProcessor)
        {
            _ocrProcessor = ocrProcessor;
        }

        public Dictionary<string, int> RecognizeWords(string imagePath)
        {
            var words = _ocrProcessor.ExtractTextFromImage(imagePath);
            return ProcessWords(words);
        }

        private Dictionary<string, int> ProcessWords(IEnumerable<string> words)
        {
            var wordCount = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

            foreach (var word in words)
            {
                if (wordCount.ContainsKey(word))
                {
                    wordCount[word]++;
                }
                else
                {
                    wordCount[word] = 1;
                }
            }

            return wordCount;
        }
    }
}
